package wci.frontend.c.tokens;

import static wci.frontend.c.CTokenType.IDENTIFIER;
import static wci.frontend.c.CTokenType.RESERVED_WORDS;
import wci.frontend.Source;
import wci.frontend.c.CToken;
import wci.frontend.c.CTokenType;


/**
 * CWordToken
 *
 *	extracts and then represents an identifier or a
 *  keyword for the C language
 *  @author James Foley
 */
public class CWordToken extends CToken
{

	public CWordToken(Source source)
		throws Exception
	{
		super(source);
	}
	
	protected  void extract() throws Exception
	{
		StringBuilder textBuffer = new StringBuilder();
		char currentChar = currentChar();
	
		// Get the word characters (letter or digit).  The scanner has
        // already determined that the first character is a letter.
        while (Character.isLetterOrDigit(currentChar)) {
            textBuffer.append(currentChar);
            currentChar = nextChar();  // consume character
        }

        text = textBuffer.toString();

        // Is it a reserved word or an identifier?
        type = (RESERVED_WORDS.contains(text))
               ? CTokenType.valueOf(text.toUpperCase())  // reserved word
               : IDENTIFIER;   
	}
	
}